#include <linux/init.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/kobject.h>


// 定义1个kobject指针变量
struct kobject *mykobject01;


// 自定义属性 show
ssize_t show_myvalue1(struct kobject *kobj, struct kobj_attribute *attr,
			char *buf)
{
    ssize_t count;
    count = sprintf(buf, "show_myvalue1\n");

    return count;
}

ssize_t store_myvalue1(struct kobject *kobj, struct kobj_attribute *attr,
            const char *buf, size_t count)
{
    printk(KERN_EMERG "store_myvalue1 buf is :%s\n", buf);
    return count;
}

ssize_t show_myvalue2(struct kobject *kobj, struct kobj_attribute *attr,
			char *buf)
{
    ssize_t count;
    count = sprintf(buf, "show_myvalue2\n");

    return count;
}

ssize_t store_myvalue2(struct kobject *kobj, struct kobj_attribute *attr,
            const char *buf, size_t count)
{
    printk(KERN_EMERG "store_myvalue2 buf is :%s\n", buf);
    return count;
}

// 定义attribute属性
struct kobj_attribute value1 = __ATTR(value1, 0664, show_myvalue1, store_myvalue1);
struct kobj_attribute value2 = __ATTR(value2, 0664, show_myvalue2, store_myvalue2);



static int __init mykobject_init(void)
{   
    int ret;
    mykobject01 =  kobject_create_and_add("mykboject01", NULL);
    ret = sysfs_create_file(mykobject01,&value1.attr);
    ret = sysfs_create_file(mykobject01,&value2.attr);

    printk(KERN_EMERG "mykobject_init OK!\n");
    return 0;
}

static void __exit mykojbect_exit(void)
{
    // 释放之前创建的kobject对象
    kobject_put(mykobject01);
    printk(KERN_EMERG "mykojbect_exit OK!\n");  
}



module_init(mykobject_init);
module_exit(mykojbect_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Treasure1029");
